<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Banner_model extends CI_Model {

    const TABLE = 'banner';
    const TABLE_CAT = 'banner_categoria';
    const TABLE_STATS = 'banner_stats';

    function __construct() {
        parent::__construct();
        $this->defaultDB = $this->load->database('default', TRUE);
    }

    function getAllCategorias() {
        $this->db->order_by("nome ASC");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblBannerCategoria'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    function getCategoria($id) {
        $this->db->where('id', $id);
        return $this->db->get($this->config->item('dbCliente') . $this->config->item('tblBanner'));
    }

    function getRandomBanner($id) {
        $this->db->where('categoria_id', $id);
        $this->db->where('status', '1');
        $this->db->order_by("id", "random");
        return $this->db->get($this->config->item('dbCliente') . $this->config->item('tblBanner'));
    }

    function getAllBanners() {
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblBannerCategoria'), $this->config->item('dbCliente') . $this->config->item('tblBannerCategoria') . '.id = ' . $this->config->item('dbCliente') . $this->config->item('tblBanner') . '.categoria_id');
        $this->db->order_by($this->config->item('dbCliente') . $this->config->item('tblBanner') . ".id DESC");
        $this->db->select($this->config->item('dbCliente') . $this->config->item('tblBanner') . '.*,' . $this->config->item('dbCliente') . $this->config->item('tblBannerCategoria') . '.nome');
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblBanner'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    function alterarStatus($id, $array) {
        $this->db->where('id', $id);
        $this->db->update($this->config->item('dbCliente') . $this->config->item('tblBanner'), $array);
        return true;
    }

    function getBannersCategoria($id) {
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblBannerCategoria'), $this->config->item('dbCliente') . $this->config->item('tblBannerCategoria') . '.id = ' . $this->config->item('dbCliente') . $this->config->item('tblBanner') . '.categoria_id');
        $this->db->order_by($this->config->item('dbCliente') . $this->config->item('tblBanner') . ".id DESC");
        $this->db->select($this->config->item('dbCliente') . $this->config->item('tblBanner') . '.*,' . $this->config->item('dbCliente') . $this->config->item('tblBannerCategoria') . '.nome');
        $this->db->where($this->config->item('dbCliente') . $this->config->item('tblBanner') . '.categoria_id', $id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblBanner'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

    function getBanner($id) {
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblBannerCategoria'), $this->config->item('dbCliente') . $this->config->item('tblBannerCategoria') . '.id = ' . $this->config->item('dbCliente') . $this->config->item('tblBanner') . '.categoria_id');
        $this->db->select($this->config->item('dbCliente') . $this->config->item('tblBanner') . '.*,' . $this->config->item('dbCliente') . $this->config->item('tblBannerCategoria') . '.nome');
        $this->db->where($this->config->item('dbCliente') . $this->config->item('tblBanner') . '.id', $id);
        return $this->db->get($this->config->item('dbCliente') . $this->config->item('tblBanner'));
    }

    function getBannerStats($id) {
        $this->db->where('banner_id', $id);
        return $this->db->get($this->config->item('dbCliente') . $this->config->item('tblBannerStats'));
    }

    function getBannerViews($id) {
        $this->defaultDB->where('banner_id', $id);
        $this->defaultDB->where('tipo', 0);
        return $this->defaultDB->count_all_results(self::TABLE_STATS);
    }

    function getBannerCliques($id) {
        $this->db->where('banner_id', $id);
        $this->db->where('tipo', 1);
        return $this->db->count_all_results($this->config->item('dbCliente') . $this->config->item('tblBannerStats'));
    }

    function inserirBanner($array) {
        if ($this->defaultDB->insert(self::TABLE, $array)) {
            return true;
        }
        return false;
    }

    function inserirStats($array) {
        $this->db->insert($this->config->item('dbCliente') . $this->config->item('tblBannerStats'), $array);
    }

    function atualizarBanner($id, $array) {
        $this->defaultDB->where('id', $id);
        $this->defaultDB->update(self::TABLE, $array);
        return true;
    }

}